package com.taiyi.renaissancemall.ui.mine.order.sale.refund;

import android.content.Intent;

import com.taiyi.renaissancemall.Utils.Utils;
import com.taiyi.renaissancemall.base.BasePresent;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.BaseResultBean;
import com.taiyi.renaissancemall.bean.EmptyBean;
import com.taiyi.renaissancemall.bean.FindExpressBean;
import com.taiyi.renaissancemall.bean.OrderDetailBean;
import com.taiyi.renaissancemall.bean.RefundResultBean;
import com.taiyi.renaissancemall.bean.VerifiedBean;
import com.taiyi.renaissancemall.net.BaseUrl;
import com.taiyi.renaissancemall.net.HttpUtils;
import com.taiyi.renaissancemall.net.callbck.JsonCallback;
import com.taiyi.renaissancemall.ui.login.LoginActivity;
import com.taiyi.renaissancemall.ui.mine.order.sale.ApplySaleView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Response;

public class ApplyRefundPresent extends BasePresent<ApplyRefundView> {

    //获取退换详情
    public void getOrderDetail(String ordercommodityid) {
        Map<String, String> map = new HashMap<>();
        map.put("ordercommodityid", ordercommodityid);
        HttpUtils.postRequest(BaseUrl.GET_ORDER_DETAIL, this, map, new JsonCallback<BaseResultBean<OrderDetailBean>>() {
            @Override
            public BaseResultBean<OrderDetailBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<OrderDetailBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getOrderDetailSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getOrderDetailFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<OrderDetailBean>> response) {
                super.onError(response);
            }
        });
    }

    //获取退换原因
    public void getRefundResult() {
        Map<String, String> map = new HashMap<>();
        HttpUtils.postRequest(BaseUrl.TUI_YUAN_YIN, this, map, new JsonCallback<BaseResultBean<RefundResultBean>>() {
            @Override
            public BaseResultBean<RefundResultBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<RefundResultBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getRefundResultSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getRefundResultFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<RefundResultBean>> response) {
                super.onError(response);
            }
        });
    }

    //退款，退货，换货
    public void toApplyRefund(String ordercommodityid, String returntype, String ifgetgoods, String return_remark, String returnurl, String typeCode) {
        Map<String, String> map = new HashMap<>();
        map.put("ordercommodityid", ordercommodityid);
        map.put("returnflag", typeCode);//1 我要退款（无需退货）2 退货退还 3 换货
        map.put("returntype", returntype);
        map.put("ifgetgoods", ifgetgoods);
        map.put("return_remark", return_remark);
        map.put("returnurl", returnurl);
        HttpUtils.postRequest(BaseUrl.SH_ORDER, this, map, new JsonCallback<BaseResultBean<VerifiedBean>>() {
            @Override
            public BaseResultBean<VerifiedBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<VerifiedBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.toApplyRefundSuccess(response.body().msg_cn);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.toApplyRefundFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<VerifiedBean>> response) {
                super.onError(response);
            }
        });
    }

    //填写物流单号
    public void toEditExpress(String ordercommodityid, String sendexpressagename, String sendexpressageno, String sendcode, String ifflag) {
        Map<String, String> map = new HashMap<>();
        map.put("ordercommodityid", ordercommodityid);
        map.put("sendexpressagename", sendexpressagename);
        map.put("sendexpressageno", sendexpressageno);
        map.put("sendcode", sendcode);
        map.put("ifflag", ifflag);
        HttpUtils.postRequest(BaseUrl.ADD_EXPRESS, this, map, new JsonCallback<BaseResultBean<VerifiedBean>>() {
            @Override
            public BaseResultBean<VerifiedBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<VerifiedBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.editExpressSuccess(response.body().msg_cn);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.editExpressFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<VerifiedBean>> response) {
                super.onError(response);
            }
        });
    }

    //快递公司列表
    public void findExpress() {
        Map<String, String> map = new HashMap<>();
        HttpUtils.postRequest(BaseUrl.FIND_EXPRESS, this, map, new JsonCallback<BaseResultBean<List<FindExpressBean>>>() {
            @Override
            public BaseResultBean<List<FindExpressBean>> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<List<FindExpressBean>>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.findExpressSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.findExpressFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<List<FindExpressBean>>> response) {
                super.onError(response);
            }
        });
    }

}
